package GateKeeper;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

import Database.*;
import Exception.*;
import General.*;
import GenericMethod.*;
import Manager.*;
import Teacher.*;

public class WindowGK extends JFrame
{
	//Variable Declaration
	private CallDB callDB;
	private Connection connection;
	private SetDimension setDimension;
	
	private Container container;
	private AuthenticationPanel authenticationPanel;
	private ChoiceApiPanel choiceApiPanel;
	private WindowTeacher winTeacher;
	private WindowManager winManager;
	private MyTableModel myTableModel;
	
	private JLabel labelBackground;	
	private boolean choiceApiPanelFlag = false;
	private int wait = 0;
	private int test = 0;
	private String pathElsa = "//localhost:3306/elsa", user = "root", password = "";
	
	//Constructor
	public WindowGK() throws LoadDriverException, OpenConnectionException
	{
		//Setting Window
		super("Gatekeeper - Elsa Authentication"); //Set the title
		
		setDimension = new SetDimension();
		setDimension.setBoundFrame(this, 366, 568);

		callDB = new CallDB(); //Initialize a CallDB object		
		connection = callDB.openConnection(pathElsa, user, password);//Open a connection
		
		MyWindowListener windowListener = new MyWindowListener();
		this.addWindowListener(windowListener);

		createAuthenticationPanel();

		//Container
		container = new Container();
		container.setLayout(new BorderLayout());
		container = getContentPane();
		
		container.add(authenticationPanel, BorderLayout.CENTER);
		
		setResizable(false);
		setVisible(true); //Make the window visible
	}
		
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(choiceApiPanelFlag == true)
				{
					if(arg0.getSource() == choiceApiPanel.getButtonApiTeacher())
							winTeacher = new WindowTeacher();
					else if(arg0.getSource() == choiceApiPanel.getButtonApiManager())
						winManager = new WindowManager();
					dispose();
				}				
				else if((arg0.getSource() == authenticationPanel.getButtonConnection() || arg0.getSource() == authenticationPanel.getFieldPassword()) && authenticationPanel.getFieldLogin().getText().isEmpty() == false && new String(authenticationPanel.getFieldPassword().getPassword()).isEmpty() == false) //If all fields are filled in
				{
					authenticationPanel.getButtonConnection().setEnabled(false);
					authenticationPanel.verifyAuthentication(connection);
					
					if(authenticationPanel.getAuthorize() == true)
						launchApi();
					else
					{
						authenticationPanel.getButtonConnection().setEnabled(true);
						test++;
						if(test == 3)
						{
							wait += 10;
							JOptionPane.showMessageDialog(null, "You tried to force or bypass the system.\nWait " + wait + " seconds !", "Error", JOptionPane.INFORMATION_MESSAGE); //Show count message
							Thread.sleep(wait*1000);
							test = 0;
						}
					}
				}
				else
					throw new Layer8DataException();
			}
			catch(CheckDataException | LoadDriverException | InterruptedException | Layer8DataException | OpenConnectionException error)
			{
				TreatError.showError(error);
			}	
		}
	}
	
	public void launchApi() throws LoadDriverException, CheckDataException, OpenConnectionException
	{
		if(GeneralMethod.getSession().getManager() == true) //If the account is a manager type
			choiceApiPanel();
		else	
			winTeacher = new WindowTeacher(); //If the account is a teacher type
	}
	
	public void choiceApiPanel()
	{
		choiceApiPanel = new ChoiceApiPanel();
		choiceApiPanelFlag = true;
		
		container.remove(authenticationPanel);
		container.add(choiceApiPanel, BorderLayout.CENTER);
		
		MyActionListener actionListener = new MyActionListener();
		choiceApiPanel.getButtonApiTeacher().addActionListener(actionListener);
		choiceApiPanel.getButtonApiManager().addActionListener(actionListener);
		
		validate();
	}
	
	public void createAuthenticationPanel()
	{
		MyActionListener actionListener = new MyActionListener();
		
		authenticationPanel = new AuthenticationPanel(callDB, connection); //Creation of the authentication panel
		
		authenticationPanel.getButtonConnection().addActionListener(actionListener);
		authenticationPanel.getFieldPassword().addActionListener(actionListener);
	}

	private class MyWindowListener extends WindowAdapter{//If user close the main window
		public void windowClosing(WindowEvent arg0){try{
			callDB.closeConnection(connection);
		}catch(CloseConnectionException error){
			TreatError.showError(error);}
			System.exit(0);}}
}
